package com.jxinternet.platform.services.service.impl.steps;

import com.jxinternet.platform.services.entity.domian.Fish;
import com.jxinternet.platform.services.entity.domian.Message;
import com.jxinternet.platform.services.entity.domian.User;
import com.jxinternet.platform.services.entity.domian.Wealth;
import com.jxinternet.platform.services.entity.protobuf.BattleProtos.BattleRequest;
import com.jxinternet.platform.services.protocol.BaseProtocol;
import com.jxinternet.platform.services.protocol.Constants;
import com.jxinternet.platform.services.repository.cassandra.FishCassandraRepository;
import com.jxinternet.platform.services.repository.cassandra.UserCassandraRepository;
import com.jxinternet.platform.services.repository.cassandra.WealthCassandraRepository;
import com.jxinternet.platform.services.repository.convert.FishContert;
import com.jxinternet.platform.services.repository.convert.UserConvert;
import com.jxinternet.platform.services.repository.convert.WealthContert;
import com.jxinternet.platform.services.util.LotteryUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 作战分步骤
 * Created by gold-jx on 2017/7/19.
 */
@Slf4j
@Component
public class BattleSteps {

    @Autowired
    private UserCassandraRepository userCassandraRepository;
    @Autowired
    private WealthCassandraRepository wealthCassandraRepository;
    @Autowired
    private FishCassandraRepository fishCassandraRepository;

    @Autowired
    private UserConvert userConvert;
    @Autowired
    private WealthContert wealthContert;
    @Autowired
    private FishContert fishContert;




    /**
     * step1
     * 验证作战请求信息
     */
    public Message step1_validateRequest(BattleRequest battleRequest, String userName) {
        Message message = new Message();
        try {
            //1.用户名是否为空
            if ("".equals(userName) || userName == null) {
                message.setCode(BaseProtocol.Battle_Invalid_UserName_IsNull_Code);
                message.setRes(BaseProtocol.Battle_Invalid_UserName_IsNull_Res);
                //log.error(message.getRes(), message.getCode());
                return message;
            }
            //2.金币是否为0
            if (battleRequest.getPoint() <= 0) {
                message.setCode(BaseProtocol.Battle_Invalid_GoldCount_Exception_Code);
                message.setRes(BaseProtocol.Battle_Invalid_GoldCount_Exception_Res);
                //log.error(message.getRes(), message.getCode());
                return message;
            }
            return message;

        } catch (Exception e) {
            message.setCode(BaseProtocol.Battle_Step01_Exception_Code);
            message.setRes(BaseProtocol.Battle_Step01_Exception_Res);
            //log.error(message.getRes(), message.getCode());
            return message;
        }

    }

    /**
     * step2
     * 获取用户
     */
    public Message step2_validateUser(String userName) {
        Message message = new Message();
        try {

            User user= userConvert.convertCassandraDomainToModel(userCassandraRepository.findByUserName(userName));
            if (user == null) {
                message.setCode(BaseProtocol.Battle_Invalid_User_Not_Exist_Code);
                message.setRes(BaseProtocol.Battle_Invalid_User_Not_Exist_Res);
                //log.error(message.getRes(), message.getCode());
                return message;
            }
            if (!Constants.STATE_NORMAL.equals(user.getState())) {
                message.setCode(BaseProtocol.Battle_Invalid_User_Exception_Code);
                message.setRes(BaseProtocol.Battle_Invalid_User_Exception_Res);
                //log.error(message.getRes(), message.getCode());
                return message;
            }
            message.setData(user);
            return message;
        } catch (Exception e) {
            message.setCode(BaseProtocol.Battle_Step02_Exception_Code);
            message.setRes(BaseProtocol.Battle_Step02_Exception_Res);
            //log.error(message.getRes(), message.getCode());
            return message;
        }
    }

    /**
     * 暂未做
     * step3
     * 获取财富
     */
    public Message step3_validateWealth(String userName,long request_point) {

        Message message = new Message();
        try {
            Wealth wealth=wealthContert.convertCassandraDomainToModel(wealthCassandraRepository.findByUserName(userName));
            if (wealth == null) {
                message.setCode(BaseProtocol.Battle_Invalid_Wealth_Not_Exist_Code);
                message.setRes(BaseProtocol.Battle_Invalid_Wealth_Not_Exist_Res);
                //log.error(message.getRes(), message.getCode());
                return message;
            }
            if (!Constants.STATE_NORMAL.equals(wealth.getState())) {
                message.setCode(BaseProtocol.Battle_Invalid_Wealth_Exception_Code);
                message.setRes(BaseProtocol.Battle_Invalid_Wealth_Exception_Res);
                //log.error(message.getRes(), message.getCode());
                return message;
            }
            if (wealth.getRechargePoint() < request_point) {
                message.setCode(BaseProtocol.Battle_Invalid_Wealth_Point_BuGou_Code);
                message.setRes(BaseProtocol.Battle_Invalid_Wealth_Point_BuGou_Res);
                //log.error(message.getRes(), message.getCode());
                return message;
            }
            message.setData(wealth);
            return message;
        } catch (Exception e) {
            message.setCode(BaseProtocol.Battle_Step03_Exception_Code);
            message.setRes(BaseProtocol.Battle_Step03_Exception_Res);
            //log.error(message.getRes(), message.getCode());
            return message;
        }

    }

    /**
     * step4
     * 获取鱼
     */
    public Message step4_validateFish(String fishCode) {
        Message message = new Message();
        try {
            Fish fish=fishContert.convertCassandraDomainToModel(fishCassandraRepository.findByFishCode(fishCode));
            if (fish == null) {
                message.setCode(BaseProtocol.Battle_Invalid_Fish_Not_Exist_Code);
                message.setRes(BaseProtocol.Battle_Invalid_Fish_Not_Exist_Res);
                //log.error(message.getRes(), message.getCode());
                return message;
            }
            if (!Constants.STATE_NORMAL.equals(fish.getState())) {
                message.setCode(BaseProtocol.Battle_Invalid_Fish_Exception_Code);
                message.setRes(BaseProtocol.Battle_Invalid_Fish_Exception_Res);
                //log.error(message.getRes(), message.getCode());
                return message;
            }
            message.setData(fish);
            return message;
        } catch (Exception e) {
            message.setCode(BaseProtocol.Battle_Step04_Exception_Code);
            message.setRes(BaseProtocol.Battle_Step04_Exception_Res);
            //log.error(message.getRes(), message.getCode());
            return message;
        }

    }


    /**
     * step5
     * 是否被击中 true=击中，false=未击中
     */
    public Message step5_isHit(Fish fish) {
        Message message = new Message();
        try {
            double chance = fish.getChance();//概率
            Boolean flag = LotteryUtil.PercentageRandom(chance);
            message.setData(flag);
            return message;
        } catch (Exception e) {
            message.setCode(BaseProtocol.Battle_Step05_Exception_Code);
            message.setRes(BaseProtocol.Battle_Step05_Exception_Res);
            //log.error(message.getRes(), message.getCode());
            return message;

        }
    }


    /**
     * step6
     * 返回积分
     */
    public Message step6_reckonPoints(Wealth wealth, long point, Fish fish, boolean flag) {
        Message message=new Message();
        try {
            Integer point1=(flag) ? (int) (point * fish.getOdds()) : 0;
            message.setData(point1);
            return message;

        }catch (Exception e){
            message.setCode(BaseProtocol.Battle_Step06_Exception_Code);
            message.setRes(BaseProtocol.Battle_Step06_Exception_Res);
            //log.error(message.getRes(), message.getCode());
            return message;
        }

    }

    /**
     *step7
     * 更新积分
     */
    public Message step7_updatePoint(Wealth wealth, long rarningsPoint) {
        Message message=new Message();
        try {
            wealth.setRechargePoint(rarningsPoint);
            wealthCassandraRepository.save(wealthContert.convertCassandra(wealth));
            return message;

        }catch (Exception e){
            message.setCode(BaseProtocol.Battle_Step07_Exception_Code);
            message.setRes(BaseProtocol.Battle_Step07_Exception_Res);
            //log.error(message.getRes(), message.getCode());
            return message;

        }

    }

}
